package com.atguigui.leetcode1;

import java.util.*;

/**
 * 2363.合并相似的物品
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/2/28 8:58
 */
public class P2363MergeSimilarItems {
    public static void main(String[] args) {
        Solution solution = new P2363MergeSimilarItems().new Solution();
        // TO TEST
    }

    class Solution {
        public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {

            List<List<Integer>> ans = new ArrayList<>();
            HashMap<Integer, Integer> map = new HashMap<>();
            for (int[] ints : items1) {
                map.put(ints[0], map.getOrDefault(ints[0], 0) + ints[1]);
            }
            for (int[] ints : items2) {
                map.put(ints[0], map.getOrDefault(ints[0], 0) + ints[1]);
            }

            //使用流式map转list
            map.forEach((k, v) -> {
                ArrayList<Integer> list = new ArrayList<>();
                list.add(k);
                list.add(v);
                ans.add(list);
            });

            //自定义排序
            Collections.sort(ans, new Comparator<List<Integer>>() {
                @Override
                public int compare(List<Integer> o1, List<Integer> o2) {
                    return o1.get(0) - o2.get(0);
                }
            });


            return ans;
        }
    }

}
